package cn.com.lsy.product.lepao.service.impl;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.model.dao.*;
import cn.com.lsy.product.lepao.model.entities.*;
import cn.com.lsy.product.lepao.model.mt.dao.MtAreaMapper;
import cn.com.lsy.product.lepao.model.mt.dao.MtStoreMapper;
import cn.com.lsy.product.lepao.model.mt.dao.MtStorePhotoMapper;
import cn.com.lsy.product.lepao.model.mt.entities.CityListDto;
import cn.com.lsy.product.lepao.model.mt.entities.GetsStoreAlbumDto;
import cn.com.lsy.product.lepao.model.mt.entities.StoreIconListDto;
import cn.com.lsy.product.lepao.service.IAccessoryService;
import cn.com.lsy.product.lepao.service.IStoreService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoreService implements IStoreService {

    @Autowired
    MtAreaMapper mtAreaMapper;
    @Autowired
    LpAreaTblMapper lpAreaTblMapper;
    @Autowired
    MtStoreMapper mtStoreMapper;
    @Autowired
    LpStoreTblMapper lpStoreTblMapper;
    @Autowired
    MtStorePhotoMapper mtStorePhotoMapper;
    @Autowired
    LpStorePhotoTblMapper lpStorePhotoTblMapper;
    @Autowired
    IAccessoryService iAccessoryService;
    @Autowired
    LpStoresOnlineTblMapper lpStoresOnlineTblMapper;
    @Autowired
    LpStoreIconTblMapper lpStoreIconTblMapper;
    @Autowired
    LpIconTblMapper lpIconTblMapper;
    @Autowired
    LpGoodsTblMapper lpGoodsTblMapper;

    @Override
    public List<CityListDto> selectCityListByPage(int start, int end, int parentId, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("start", start);
        where.put("end", end);
        where.put("parentId", parentId);
        where.put("searchCondition", searchCondition);

        return mtAreaMapper.selectByPage(where);
    }

    @Override
    public int selectCityListByCount(int parentId, String searchCondition) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("parentId", parentId);
        where.put("searchCondition", searchCondition);

        return mtAreaMapper.selectByCount(where);
    }

    @Override
    public boolean updateArea(LpAreaTbl param) {

        if (lpAreaTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<LpStoreTbl> selectByPage(int start, int end, String searchCondition, List<Integer> storeId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("start", start);
        where.put("end", end);
        where.put("searchCondition", searchCondition);
        where.put("storeId", storeId);

        return mtStoreMapper.selectByPage(where);
    }

    @Override
    public int selectByCount(String searchCondition, List<Integer> storeId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("searchCondition", searchCondition);
        where.put("storeId", storeId);

        return mtStoreMapper.selectByCount(where);
    }

    @Override
    public LpStoreTbl selectInfo(int id) {

        return lpStoreTblMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateStore(LpStoreTbl param) {

        if (lpStoreTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean addStore(LpStoreTbl param) {

        if (lpStoreTblMapper.insertSelective(param) > 0) {

            //新增门店日卡
            LpGoodsTblWithBLOBs lpGoodsTbl = new LpGoodsTblWithBLOBs();
            lpGoodsTbl.setGoodsName(param.getStoreName() + "-单次收费");
            lpGoodsTbl.setAmount(new BigDecimal("9.9"));
            lpGoodsTbl.setIsBalance(Constant.getBoolean.yes);
            lpGoodsTbl.setType(Constant.goodsType.dayCard);
            try {
                lpGoodsTbl.setInsertTime(CommonFunctionUtil.getDateTime(Constant.formatType.second));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            lpGoodsTbl.setIsDelete(Constant.getBoolean.no);
            lpGoodsTbl.setRemark(param.getStoreName() + "日卡");
            lpGoodsTbl.setStoreId(param.getId());
            lpGoodsTbl.setOriginalPrice(new BigDecimal("0"));
            lpGoodsTbl.setImgId(0);
            lpGoodsTbl.setParentId(0);

            lpGoodsTblMapper.insertSelective(lpGoodsTbl);

            return true;

        } else {

            return false;
        }
    }

    @Override
    public List<GetsStoreAlbumDto> selectStorePhoto(int id) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("storeId", id);

        return mtStorePhotoMapper.selectStoreAlbum(where);
    }

    @Override
    public boolean updateStorePhoto(LpStorePhotoTbl param) {

        if (lpStorePhotoTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    @Transactional
    public boolean insertStorePhoto(int sort, int storeId, String url) {

        //写入图片信息
        LpAccessoryTbl lpAccessoryTbl = new LpAccessoryTbl();
        lpAccessoryTbl.setUrl(url);
        lpAccessoryTbl.setName("门店相册");
        int photoId = iAccessoryService.insert(lpAccessoryTbl);

        if (photoId > 0) {

            //写入门店相册表信息
            LpStorePhotoTbl lpStorePhotoTbl = new LpStorePhotoTbl();
            lpStorePhotoTbl.setPhotoId(photoId);
            lpStorePhotoTbl.setSort(sort);
            lpStorePhotoTbl.setStoreId(storeId);

            if (lpStorePhotoTblMapper.insertSelective(lpStorePhotoTbl) > 0) {

                return true;

            } else {

                throw new RuntimeException("添加失败");
            }

        } else {

            return false;
        }
    }

    @Override
    @Transactional
    public boolean delStorePhoto(int id, int photoId) {

        if (iAccessoryService.delete(photoId)) {

            if (lpStorePhotoTblMapper.deleteByPrimaryKey(id) > 0) {

                return true;

            } else {

                throw new RuntimeException("删除失败");
            }

        } else {

            return false;
        }
    }

    @Override
    public List<LpAreaTbl> selectAreaList(int leve) {

        LpAreaTblExample example = new LpAreaTblExample();
        example.createCriteria().andLevelEqualTo(String.valueOf(leve));

        return lpAreaTblMapper.selectByExample(example);
    }

    @Override
    public boolean delStoreOnLine(int storeId) {

        LpStoresOnlineTblExample example = new LpStoresOnlineTblExample();

        example.createCriteria().andStoreIdEqualTo(storeId);

        if (lpStoresOnlineTblMapper.deleteByExample(example) > 0) {

            return true;

        } else {

            return false;
        }

    }

    @Override
    public List<StoreIconListDto> selectStoreIconList(int storeId) {

        Map<String, Object> where = new HashMap<String, Object>();
        where.put("storeId", storeId);

        return mtStoreMapper.selectStoreIconList(where);
    }

    @Override
    public boolean updateStoreIcon(LpStoreIconTbl param) {

        if (lpStoreIconTblMapper.updateByPrimaryKeySelective(param) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean delStoreIcon(int id) {

        if (lpStoreIconTblMapper.deleteByPrimaryKey(id) > 0) {

            return true;

        } else {

            return false;
        }
    }

    @Override
    public boolean addStoreIcon(LpStoreIconTbl param) {

        if (lpStoreIconTblMapper.insertSelective(param) > 0) {

            return true;

        } else {

            return false;
        }

    }

    @Override
    public List<LpStoreIconTbl> selectStoreIcon(int storeId, int iconId) {

        LpStoreIconTblExample example = new LpStoreIconTblExample();
        example.createCriteria().andStoreIdEqualTo(storeId).andIconIdEqualTo(iconId);

        return lpStoreIconTblMapper.selectByExample(example);
    }

    @Override
    public long countStoreIcon(int storeId) {

        LpStoreIconTblExample example = new LpStoreIconTblExample();
        example.createCriteria().andStoreIdEqualTo(storeId);

        return lpStoreIconTblMapper.countByExample(example);
    }

    @Override
    public List<LpIconTbl> selectIconList() {

        LpIconTblExample example = new LpIconTblExample();

        return lpIconTblMapper.selectByExample(example);

    }

    @Override
    public List<LpStoreTbl> selectList() {

        LpStoreTblExample example = new LpStoreTblExample();
        example.createCriteria().andStatusEqualTo(Constant.status.enable);

        return lpStoreTblMapper.selectByExample(example);
    }

    @Override
    public List<LpStoresOnlineTbl> selectStoresOnlineList(int storeId) {

        LpStoresOnlineTblExample example = new LpStoresOnlineTblExample();
        example.createCriteria().andStoreIdEqualTo(storeId);

        return lpStoresOnlineTblMapper.selectByExample(example);

    }
}
